package com.laihui.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.laihui.MyApplication;
import com.laihui.activity.R;
import com.laihui.adapters.AccountLogAdapter;
import com.laihui.base.BaseFragment;
import com.laihui.entity.AccountLogList;
import com.laihui.utils.YXDate;
import com.laihui.view.TopBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/1/5.
 */

public class RechargeLogFragment extends BaseFragment {
    @BindView(R.id.topbar)
    TopBar topbar;
    @BindView(R.id.easy_recycler_view)
    EasyRecyclerView easyRecyclerView;
    private AccountLogAdapter adapter;
    AccountLogList lastMoneyRecordDetail;
    private MyApplication application;
    private int currentPage = 1 ;

    @Override
    public void init() {
        application = (MyApplication)getActivity().getApplication();
        adapter = new AccountLogAdapter(getContext()) ;

        getMoneyRecordDetail(true);


        easyRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//          easyRecyclerView.addItemDecoration(new DividerDecoration(R.color.material_white, 1));
        easyRecyclerView.setAdapter(adapter);

        easyRecyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1 ;
                getMoneyRecordDetail(true);
            }
        });

        adapter.setMore(R.layout.load_more_layout, new RecyclerArrayAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                currentPage ++ ;
                getMoneyRecordDetail(false);
            }
        }) ;
    }
    private void getMoneyRecordDetail(final boolean isRefresh){
        Map<String,String> map = new HashMap<>();
        map.put("member_id", application.userId);
        map.put("currentPage",currentPage+"");
        map.put("logType","recharge");
//        map.put("type","1");
        application.apiService.accountlog(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AccountLogList>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("TAG", "onError: "+e.toString());
                    }

                    @Override
                    public void onNext(AccountLogList accountLogList) {
                        // if(currentPage>1){
                        List<AccountLogList.AccountBean> list = new ArrayList<>();
                        List<AccountLogList.AccountBean> logs = accountLogList.list;
                        for(int i=0;i<logs.size();i++){
                            Log.e("error","0");
                            if(currentPage>1&i==0){
                                Log.e("error","1");
                                // 如果currentPage大于1 那么要判断 第一条上一页最后一条数据
                                AccountLogList.AccountBean lastbean  = lastMoneyRecordDetail.list.get(lastMoneyRecordDetail.list.size()-1);
                                Log.e("error","2");
                                AccountLogList.AccountBean bean  = logs.get(i);
                                if(YXDate.getYYYYMM(logs.get(i).addtime)<YXDate.getYYYYMM(lastbean.addtime)){
                                    bean.month=(YXDate.getYYYYMM(logs.get(i).addtime)+"");
                                    bean.time_remark =1;
                                }
                                list.add(bean);
                            }else if(currentPage==1&i==0){
                                AccountLogList.AccountBean bean  = logs.get(i);
                                bean.month = (YXDate.getYYYYMM(logs.get(i).addtime)+"");
                                bean.time_remark = 1;
                                list.add(bean);
                            }
                            else if(i>0){
                                AccountLogList.AccountBean bean  = logs.get(i);
                                AccountLogList.AccountBean lastbean  = logs.get(i-1);
                                if(YXDate.getYYYYMM(bean.addtime)<YXDate.getYYYYMM(lastbean.addtime)){
                                    bean.month =(YXDate.getYYYYMM(logs.get(i).addtime)+"");
                                    bean.time_remark = 1;
                                }
                                list.add(bean);
                            }

                        }
                        if (isRefresh){
                            adapter.clear();
                            adapter.addAll(list);
                        }else{


                            adapter.addAll(list);
                        }
                        lastMoneyRecordDetail = accountLogList;
                    }
                });
    }
    @Override
    public int getFragmentId() {
        return R.layout.activity_money_record_detail;
    }

    @Override
    public Object bindFragment() {
        return this;
    }
}
